package com.eden.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eden.config.Chart;
import com.eden.config.Configuration;
import com.eden.config.Score;
import com.eden.context.AppContext;
import com.eden.util.MessageUtil;
import com.eden.util.NumberFormatUtil;
import com.eden.util.TypeConvertUtil;

public class SetupView extends JDialog {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(SetupView.class) ;
	
	private final JPanel contentPanel = new JPanel();
	private JPanel panel_1;
	private JPanel panel;
	private JTextField chartTitleField;
	private JTextField chartXNameField;
	private JTextField chartYNameField;
	private JComboBox<Object> chartTypeBox;
	
	private Window self ;
	
	private Configuration configuration ;
	List<Map<String, Component>> compList ;
	private JScrollPane scrollPane;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AppContext.init() ;
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) ;
			SetupView dialog = new SetupView(null , true);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SetupView(Frame parent , boolean modal) {
		super(parent , modal) ;
		self = this ;
		init();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setResizable(false);
		setBounds(100, 100, 681, 456);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			panel_1 = new JPanel();
			panel_1.setPreferredSize(new Dimension(200, 300));
		}
		{
			panel = new JPanel();
			panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "\u5206\u5E03\u56FE", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
			panel.setPreferredSize(new Dimension(100, 100));
		}
		
		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
		flowLayout.setHgap(20);
		flowLayout.setVgap(20);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(21)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
							.addContainerGap(416, Short.MAX_VALUE)
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 310, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(10)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE)))
					.addGap(33)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(29, Short.MAX_VALUE))
		);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double percent = 0 ;
				log.info("read config and persistent...") ;
				for(Map<String, Component> m : compList) {
					for(Score score : configuration.getScoreList()) {
						if(((JLabel)m.get("name")).getText().trim().equals(score.getName())) {
//							如果有效就保存下来
							if(((JCheckBox)m.get("valid")).isSelected()) {
								score.setPercent(TypeConvertUtil.toDouble(((JTextField)m.get("percent")).getText()) / 100.0 );
								percent =TypeConvertUtil.toDouble( NumberFormatUtil.formatDouble(percent + score.getPercent())) ;
								score.setAnalyze(((JCheckBox)m.get("analyze")).isSelected()) ;
								score.setShow(((JCheckBox)m.get("show")).isSelected()) ;
								score.setValid(((JCheckBox)m.get("valid")).isSelected()) ;
							}
						}
					}
				}
				if(percent != 1.0) {
					MessageUtil.showMessage(self, "分数分布百分比错误!") ;
					return ;
				}
				
				Chart chartConfig = configuration.getChart() ;
				chartConfig.setTitle(chartTitleField.getText().trim()) ;
				chartConfig.setxName(chartXNameField.getText().trim()) ;
				chartConfig.setyName(chartYNameField.getText().trim()) ;
				chartConfig.setType(chartTypeBox.getSelectedIndex()) ;
				AppContext.setConfiguration(configuration) ;
				dispose() ;
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
		panel_2.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose() ;
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12));
		panel_2.add(btnNewButton_1);
		
		JLabel label = new JLabel("图表类型:");
		label.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JLabel lblNewLabel_1 = new JLabel("标题");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JLabel lblNewLabel_2 = new JLabel("x轴名称");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JLabel lblNewLabel_3 = new JLabel("y轴名称");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 12));
		
		chartTitleField = new JTextField();
		chartTitleField.setFont(new Font("宋体", Font.PLAIN, 12));
		chartTitleField.setColumns(10);
		
		chartXNameField = new JTextField();
		chartXNameField.setFont(new Font("宋体", Font.PLAIN, 12));
		chartXNameField.setColumns(10);
		
		chartYNameField = new JTextField();
		chartYNameField.setFont(new Font("宋体", Font.PLAIN, 12));
		chartYNameField.setColumns(10);
		
		chartTypeBox = new JComboBox();
		chartTypeBox.setFont(new Font("宋体", Font.PLAIN, 12));
		chartTypeBox.setModel(new DefaultComboBoxModel(new String[] {"柱状图", "3D柱状图", "曲线图"}));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(label)
						.addComponent(lblNewLabel_1)
						.addComponent(lblNewLabel_2)
						.addComponent(lblNewLabel_3))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(chartYNameField, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
						.addComponent(chartXNameField, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
						.addComponent(chartTitleField, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
						.addComponent(chartTypeBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap(76, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(chartTypeBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(chartTitleField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(chartXNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(chartYNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(114, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		scrollPane = new JScrollPane();
		scrollPane.setViewportBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.CYAN));
		scrollPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "\u6210\u7EE9\u5206\u5E03", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		contentPanel.setLayout(gl_contentPanel);
		initScore() ;
		initChart() ;
	}
	public void init(){
		configuration = AppContext.getConfiguration() ;
	}
	
	public void initChart() {
		Configuration configuration = AppContext.getConfiguration() ;
		Chart chart = configuration.getChart() ;
		chartTypeBox.setSelectedIndex(chart.getType()) ;
		chartTitleField.setText(chart.getTitle()) ;
		chartXNameField.setText(chart.getxName()) ;
		chartYNameField.setText(chart.getyName()) ;
	}
	
	public void initScore(){
		JPanel panel_3 = new JPanel();
		
		List<Score> scoreList = configuration.getScoreList() ;
		JPanel headerView = new JPanel() ;
		headerView.setLayout(new GridLayout(1, 5, 10, 2)) ;
		headerView.add(new JLabel("名称")) ;
		headerView.add(new JLabel("图表显示")) ;
		headerView.add(new JLabel("输入显示")) ;
		headerView.add(new JLabel("有效")) ;
		headerView.add(new JLabel("百分比")) ;
		scrollPane.setColumnHeaderView(headerView) ;
		
		compList = new ArrayList<Map<String,Component>>() ;
		Map<String, Component> item = null ;
		for(int i = 0 , length = scoreList.size() ; i < length ; i++) {
			item = new HashMap<String, Component>() ;
			Score score = scoreList.get(i) ;
			JLabel lblNewLabel = new JLabel(score.getName());
			lblNewLabel.setPreferredSize(new Dimension(15, 20)) ;
			panel_3.add(lblNewLabel);
			item.put("name" , lblNewLabel) ;
			
			JCheckBox chckbxNewCheckBox = new JCheckBox("" , score.isAnalyze());
			chckbxNewCheckBox.setPreferredSize(new Dimension(15, 20)) ;
			panel_3.add(chckbxNewCheckBox);
			item.put("analyze" , chckbxNewCheckBox) ;
			
			JCheckBox chckbxNewCheckBox1 = new JCheckBox("" , score.isShow());
			chckbxNewCheckBox1.setPreferredSize(new Dimension(15, 20)) ;
			panel_3.add(chckbxNewCheckBox1);
			item.put("show" , chckbxNewCheckBox1) ;
			
			JCheckBox chckbxNewCheckBox2 = new JCheckBox("" , score.isValid());
			chckbxNewCheckBox2.setPreferredSize(new Dimension(15, 20)) ;
			panel_3.add(chckbxNewCheckBox2);
			item.put("valid" , chckbxNewCheckBox2) ;
			
			JTextField jTextField = new JTextField(5) ;
			jTextField.setPreferredSize(new Dimension(15, 20)) ;
			jTextField.setMaximumSize(new Dimension(5, 20)) ;
			jTextField.setText(TypeConvertUtil.toStr(score.getPercent() * 100)) ;
			panel_3.add(jTextField) ;
			item.put("percent" , jTextField) ;
			
			compList.add(item) ;
		}
	/*	JSeparator jSeparator = new JSeparator(JSeparator.HORIZONTAL) ;
		panel_3.add(jSeparator) ;
		jSeparator = new JSeparator(JSeparator.HORIZONTAL) ;
		panel_3.add(jSeparator) ;
		jSeparator = new JSeparator(JSeparator.HORIZONTAL) ;
		panel_3.add(jSeparator) ;
		jSeparator = new JSeparator(JSeparator.HORIZONTAL) ;
		panel_3.add(jSeparator) ;
		jSeparator = new JSeparator(JSeparator.HORIZONTAL) ;
		panel_3.add(jSeparator) ;
		JButton scoreAdd = new EButton("添加") ;
		panel_3.add(scoreAdd) ;*/
		scrollPane.setViewportView(panel_3) ;
		panel_3.setLayout(new GridLayout(0, 5, 2, 2));
	}

	public void addScore(List<Object> scoreItem) {
		
	}
}
